package com.cw.lang.common.framework.utils;

import com.cw.base.common.model.constants.RpcConstant;
import com.cw.base.common.model.dto.response.CommonResponse;
import com.cw.lang.common.enums.EnumResponseType;
import com.cw.lang.common.utils.context.ThreadContextStoreUtil;

/**
 * @author chenw
 * @version $$Id: CommonResponseUtil.java, v 0.1 2018/11/10 2:20 PM chenw Exp $$
 */
public class CommonResponseUtil {

    private CommonResponseUtil() {
    }

    /**
     * 如果rpc通信成功，业务返回类型
     *
     * @return 业务返回类型
     */
    public static EnumResponseType getRpcResponseType() {
        return (EnumResponseType) ThreadContextStoreUtil.getInstance()
            .get(RpcConstant.RPC_RESPONSE_TYPE);
    }

    /**
     * 错误code
     *
     * @return 错误code
     */
    public static String getRpcErrorCode() {
        return (String) ThreadContextStoreUtil.getInstance()
            .get(RpcConstant.RPC_ERROR_CODE);
    }

    /**
     * 错误message
     *
     * @return 错误message
     */
    public static String getRpcErrorMessage() {
        return (String) ThreadContextStoreUtil.getInstance()
            .get(RpcConstant.RPC_ERROR_MESSAGE);
    }

    /**
     * 父幂等流水号
     *
     * @return 父幂等流水号
     */
    public static String getRpcParentIdemSerialId() {
        return (String) ThreadContextStoreUtil.getInstance()
            .get(RpcConstant.RPC_PARENT_IDEM_SERIAL_ID);
    }

    /**
     * rpc通信结果
     *
     * @return rpc通信结果
     */
    public static CommonResponse getCommonResponse() {
        return CommonResponse.builder()
            .responseType(getRpcResponseType())
            .parentIdemSerialId(getRpcParentIdemSerialId())
            .errorCode(getRpcErrorCode())
            .errorMessage(getRpcErrorMessage())
            .build();
    }

    /**
     * 设置CommonResponse
     *
     * @param commonResponse CommonResponse
     */
    public static void setCommonResponse(CommonResponse commonResponse) {
        ThreadContextStoreUtil.getInstance()
            .set(RpcConstant.RPC_RESPONSE_TYPE, commonResponse.getResponseType());
        ThreadContextStoreUtil.getInstance()
            .set(RpcConstant.RPC_ERROR_CODE, commonResponse.getErrorCode());
        ThreadContextStoreUtil.getInstance()
            .set(RpcConstant.RPC_ERROR_MESSAGE, commonResponse.getErrorMessage());
        ThreadContextStoreUtil.getInstance()
            .set(RpcConstant.RPC_PARENT_IDEM_SERIAL_ID, commonResponse.getParentIdemSerialId());
    }

    /**
     * 清理线程上下文中的返回信息
     */
    public static void removeCommonResponse() {
        ThreadContextStoreUtil.getInstance()
            .removeKey(RpcConstant.RPC_PARENT_IDEM_SERIAL_ID);
    }

    /**
     * 清理线程上下文中跟rpc相关的结果信息，会保留rpc通讯中的业务信息
     */
    public static void removeCommonResponseRpcInfo() {
        ThreadContextStoreUtil.getInstance()
            .removeKey(RpcConstant.RPC_RESPONSE_TYPE);
        ThreadContextStoreUtil.getInstance()
            .removeKey(RpcConstant.RPC_ERROR_CODE);
        ThreadContextStoreUtil.getInstance()
            .removeKey(RpcConstant.RPC_ERROR_MESSAGE);

    }
}
